package cn.com.coding.app;

import cn.com.coding.common.lang.ResT;
import cn.com.coding.common.utils.PageUtil;
import cn.com.coding.system.domin.UserInfoEntity;
import cn.com.coding.system.domin.UserSchoolCheckEntity;
import cn.com.coding.system.domin.dto.UserSchoolCheckDto;
import cn.com.coding.system.service.UserSchoolCheckService;
import cn.hutool.core.map.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 用户认证
 *
 * @author mxz
 */
@RestController
@RequestMapping("cat/userschoolcheck")
public class UserSchoolCheckController extends BaseController {

    @Autowired
    private UserSchoolCheckService userSchoolCheckService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public ResT list(@RequestParam Map<String, Object> params) {
        PageUtil page = userSchoolCheckService.queryPage(params);

        return ResT.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{pid}")
    public ResT info(@PathVariable("pid") String pid) {

        UserSchoolCheckDto userSchoolCheck = userSchoolCheckService.getUserSchoolCheckDtoByPid(pid);

        return ResT.ok().setData(MapUtil.builder().put("userSchoolCheck", userSchoolCheck).build());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public ResT save(@RequestBody UserSchoolCheckEntity userSchoolCheck) {
        userSchoolCheckService.save(userSchoolCheck);

        return ResT.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public ResT update(@RequestBody UserSchoolCheckDto userSchoolCheckDto) {

        userSchoolCheckDto.setUpdateTime(new Date());
        UserInfoEntity userInfoEntity = userInfoService.getById(userSchoolCheckDto.getUserPid());

        userInfoEntity.setUserName(userSchoolCheckDto.getUserName());
        userInfoEntity.setUpdateTime(new Date());
        userInfoService.saveOrUpdate(userInfoEntity);

        userSchoolCheckService.updateById(userSchoolCheckDto);

        return ResT.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public ResT delete(@RequestBody String[] pids) {

        List<UserSchoolCheckEntity> list = new ArrayList<>(pids.length);

        Arrays.stream(pids).forEach(e -> {
            UserSchoolCheckEntity userSchoolCheckEntity = userSchoolCheckService.getById(e);
            userSchoolCheckEntity.setCheckStatus(1);
            userSchoolCheckEntity.setUpdateTime(new Date());
            list.add(userSchoolCheckEntity);
        });

        userSchoolCheckService.saveOrUpdateBatch(list);

        return ResT.ok();
    }

}
